package com.it.text;

import com.it.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class text {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        emnueStudent(list);




    }

    /**
     * 菜单
     */
    private static void emnueStudent(ArrayList<Student> list) {

        while (true) {
            //菜单
            System.out.println("--------学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择:");

            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();

            switch (choice){
                case 1:
                    getAddStudent(list);
                    break;
                case 2:
                    delStudent(list);
                    break;
                case 3:
                    updataStudent(list);
                    break;
                case 4:
                    allStidentInfors(list);
                    break;
                case 5:
                    System.out.println("感谢使用！退出系统！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请重新输入!");
            }
        }
    }

    /**
     * 更新学生数据
     */
    private static void updataStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String studentNumber = sc.next();
        int index = selectIndex(list, studentNumber);
        if (index==-1){
            System.out.println("查无此人！");
        }
        else {
            System.out.println("请输入新的学生姓名:");
            String name = sc.next();
            System.out.println("请输入新的学生年龄:");
            int age = sc.nextInt();
            System.out.println("请输入新的学生生日:");
            String brithy = sc.next();
            Student stu = new Student(studentNumber,name,age,brithy);
            list.set(index,stu);
            System.out.println("修改成功！");
        }

    }

    /**
     * 查询全部学生信息
     */
    private static void allStidentInfors(ArrayList<Student> list) {
        if (list.size() == 0){
            System.out.println("暂无此信息!");
        }
        else {

            System.out.println("学号" + "\t\t\t" + "姓名" + "\t" + "年龄" + "\t" + "生日");
            for (int i = 0; i < list.size(); i++) {
                Student stu = new Student();
                stu = list.get(i);
                System.out.println(stu.getStudentNumber() + "\t"+ stu.getName()+ "\t" + stu.getAge()+ "\t"+stu.getBrithy());
            }

        }

    }

    /**
     * 删除学生信息
     * */
    private static void delStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        while (true) {
            String studentNumber = sc.next();
            int index = selectIndex(list, studentNumber);
            if (index==-1){
                System.out.println("查无此人！请重新输入学号：");
            }
            else {
                list.remove(index);
                System.out.println("删除成功！");
                break;
            }
        }

    }

    /**
     * 查找元素在列表中的索引
     */
    private static int selectIndex(ArrayList<Student> list, String studentNumber) {
        Student stu = new Student();
        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);
            if (stu.getStudentNumber().equals(studentNumber)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 添加学生信息
     */
    private static void getAddStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String studentNumber = "";

        while (true) {
            studentNumber = sc.next();
            int index = selectIndex(list,studentNumber);
            if (index == -1){

                System.out.println("请输入学生姓名:");
                String name = sc.next();

                System.out.println("请输入学生的年龄:");
                int age = sc.nextInt();

                System.out.println("请输入学生生日:");
                String brithy = sc.next();

                Student stu = new Student(studentNumber,name,age,brithy);
                list.add(stu);
                System.out.println("添加成功！");
                break;
            }
            else {
                System.out.println("学号已存在！请重新输入：");
            }
        }

    }


}
